package org.summer.lawyer.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.summer.common.core.exception.ServiceException;
import org.summer.common.core.utils.MapstructUtils;
import org.summer.common.mybatis.core.page.PageQuery;
import org.summer.common.mybatis.core.page.TableDataInfo;
import org.summer.common.satoken.utils.LoginHelper;
import org.summer.lawyer.constant.BannerTypeConstants;
import org.summer.lawyer.domain.Banner;
import org.summer.lawyer.domain.vo.banner.BannerCreateVo;
import org.summer.lawyer.domain.vo.banner.BannerUpdateVo;
import org.summer.lawyer.domain.vo.banner.BannerVo;
import org.summer.lawyer.mapper.BannerMapper;
import org.summer.lawyer.service.IBannerService;

import java.util.Collection;

/**
 *
 */
@Service
public class BannerServiceImpl extends ServiceImpl<BannerMapper, Banner> implements IBannerService {

    @Override
    public TableDataInfo<BannerVo> selectPage(BannerVo vo, PageQuery pageQuery) {
        LambdaQueryWrapper<Banner> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StrUtil.isNotBlank(vo.getBannerType()), Banner::getBannerType, vo.getBannerType())
                .like(StrUtil.isNotBlank(vo.getTitle()), Banner::getTitle, vo.getTitle());
//        if (StrUtil.isNotBlank(pageQuery.getOrderByColumn())) {
//            queryWrapper.orderByDesc(Banner::getId);
//        } else {
//
//        }
        queryWrapper.orderByDesc(Banner::getDisplaySort);
        if (PageQuery.DEFAULT_PAGE_SIZE == pageQuery.getPageSize()) {
            return TableDataInfo.build(baseMapper.selectVoList(queryWrapper));
        }
        Page<BannerVo> page = baseMapper.selectVoPage(pageQuery.build(), queryWrapper);
        return TableDataInfo.build(page);
    }

    @Override
    public void saveBanner(BannerCreateVo vo) {
        Banner banner = new Banner();
        MapstructUtils.convert(vo, banner);
        banner.setBannerType(BannerTypeConstants.HOME);
        banner.setCreateTime(DateUtil.date());
        banner.setUpdateTime(DateUtil.date());
        if (LoginHelper.getUserId() != null) {
            banner.setCreateBy(LoginHelper.getUserId());
            banner.setUpdateBy(LoginHelper.getUserId());
        }
        baseMapper.insert(banner);
    }

    @Override
    public void updateBanner(BannerUpdateVo vo) {
        Banner banner = new Banner();
        MapstructUtils.convert(vo, banner);
        banner.setUpdateTime(DateUtil.date());
        if (LoginHelper.getUserId() != null) {
            banner.setUpdateBy(LoginHelper.getUserId());
        }
        LambdaUpdateWrapper<Banner> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.eq(Banner::getId, vo.getId());
        baseMapper.update(banner, updateWrapper);
    }

    @Override
    @DSTransactional(rollbackFor = Exception.class)
    public void deleteBanner(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            throw new ServiceException("主键不能为空");
        }
        ids.forEach(item -> {
            baseMapper.deleteById(item);
        });
    }

    @Override
    public BannerVo selectBannerVoById(Long id) {
        return baseMapper.selectVoById(id);
    }
}
